﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using OpenQA.Selenium;
using System.Threading;
using OpenQA.Selenium.Support.UI;

namespace AutoSubmit.Lib.Palsra
{
    public class Palsra : ActionPage
    {
        public override void vSubmit()
        {
            if (m_submit.m_url.Length > 0)
            {
                m_driver.Navigate().GoToUrl(m_submit.m_url);
            }

            // Select main Category
            if (m_submit.m_isCategorySelect)
            {
                if (m_submit.m_mainCategoryXPath.Length > 0)
                {
                    SelectElement catagoryMain = new SelectElement(WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_mainCategoryXPath), 5));
                    catagoryMain.SelectByText(m_submit.m_mainCategory);
                }

                if (m_submit.m_subCatagoryXPath.Length > 0)
                {
                    SelectElement catagorySub = new SelectElement(WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_subCatagoryXPath), 5));
                    Thread.Sleep(5000);
                    catagorySub.SelectByText(m_submit.m_subCatagory);
                }

            }
            else
            {
                IWebElement catagoryMain = WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_mainCategoryXPath), 5);
                catagoryMain.Click();
            }


            // Set title
            if (m_submit.m_titleXPath.Length > 0)
            {
                IWebElement titleInput = WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_titleXPath), 5);
                titleInput.SendKeys(m_submit.m_title);
            }

            // Set article body
            if (m_submit.m_bodyXPath.Length > 0)
            {
                IWebElement bodyInput = WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_bodyXPath), 5);
                bodyInput.SendKeys(m_submit.m_body + "\n\n" + m_submit.m_bio);
            }

            // Set bio
            if (m_submit.m_bioXPath.Length > 0)
            {
                IWebElement bioInput = WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_bioXPath), 5);
                bioInput.SendKeys(m_submit.m_bio);
            }

            // Set key words
            if (m_submit.m_keywordsXPath.Length > 0)
            {
                IWebElement keywordsInput = WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_keywordsXPath), 5);
                keywordsInput.SendKeys(m_submit.m_keywords);
            }

            // Set summary
            if (m_submit.m_summaryXPath.Length > 0)
            {
                IWebElement summaryInput = WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_summaryXPath), 5);
                summaryInput.SendKeys(m_submit.m_summary);
            }

            // Set author
            if (m_submit.m_authorXPath.Length > 0)
            {
                IWebElement summaryInput = WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_authorXPath), 5);
                summaryInput.Clear();
                summaryInput.SendKeys(m_submit.m_author);
            }

            // Set bio plain
            if (m_submit.m_bioPlainXPath.Length > 0)
            {
                IWebElement summaryInput = WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_bioPlainXPath), 5);
                summaryInput.SendKeys(m_submit.m_bioPlain);
            }

            // Set Web URL
            if (m_submit.m_webURLXPath.Length > 0)
            {
                IWebElement webURLInput = WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_webURLXPath), 5);
                webURLInput.SendKeys(m_submit.m_webURL);
            }


            // Set primary Search Phrase
            if (m_submit.m_primarySearchPhraseXPath.Length > 0)
            {
                IWebElement phraseInput = WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_primarySearchPhraseXPath), 5);
                phraseInput.SendKeys(m_submit.m_primarySearchPhrase);
            }

            // Submit
            IWebElement submitButton = WebDriverExtension.FindElement(m_driver, By.XPath(m_submit.m_submitButtonXPath), 5);
            if (m_submit.m_isClick)
                submitButton.Click();
            else
                submitButton.Submit();
        }

        public override String vGetArticleURL()
        {
            if (m_status != SubmitStatus.SUCCESS)
            {
                return "";
            }

            if (m_article.m_url.Length > 0)
            {
                m_driver.Navigate().GoToUrl(m_article.m_url);
            }

            // find new article
            ReadOnlyCollection<IWebElement> hrefs = WebDriverExtension.FindElements(m_driver, By.XPath(m_article.m_articlesListXPath), 5);
            foreach (IWebElement ele in hrefs)
            {
                if (ele.Text.Contains("read entire article"))
                    return ele.GetAttribute("href");
            }

            return "";
        }
    }
}
